<script setup lang='ts'>
  import { onActivated, onMounted, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, $router } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import xmImage from '@/components/xmImage/index.vue'
  import Pagination from "@/components/Pagination/index.vue";
  import messageForm from './messageForm.vue';
  const messageFormRef = ref()
  const action = reactive({
    total:0,
    activityID:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    custom_id:'' as any,
  })
  const handleForm = async(row:any = {})=>{
    const { custom_id=listQuery.custom_id } = row
    messageFormRef.value.openDialog({
      ...row,
      custom_id,
    })
  }
  const handleDelete = (row:any = {})=>{
    const { custom_id=0, id=0,game_type_text='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确定删除活动ID：${custom_id}-${game_type_text}留言板吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      gamesApi.customTicketGameDel({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const handleSerach = async()=>{
    action.list = []
    action.total = 0
    listQuery.page = 1
    await getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await gamesApi.customLybList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  let primarySet = false
  const primaryGetList = async()=>{
    if(primarySet) return
    primarySet = true
    const activityID = $router().currentRoute.value.query?.activityID || ''
    if(listQuery.custom_id != activityID && activityID) {
      listQuery.custom_id = activityID
      const loading = Toastloading()
      await getList()
      primarySet = false
      loading.close()
    }
  }
  onMounted(async()=>{
    primaryGetList()
  })
  onActivated(async()=>{
    primaryGetList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">活动ID：</div>
          <el-input type="text" v-model="listQuery.custom_id" placeholder="请输入活动ID" />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" v-rule="'stencil_ticketAdd'" @click="handleForm()">新增留言板</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="120" prop="id" label="留言板ID" />
      <el-table-column width="120" prop="custom_id" label="活动ID" />
      <el-table-column width="180" prop="game_name" label="活动名称" />
      <el-table-column width="120" prop="diamand_num" label="消耗钻石数量" />
      <el-table-column width="120" prop="full_id" label="飘屏ID" />
      <el-table-column width="180" show-overflow-tooltip prop="lyb_url" label="动画" />
      <el-table-column min-width="120" prop="content" label="留言板内容" />
      <el-table-column width="180" fixed="right" label="操作">
        <template #default="scoped">
          <el-button type="primary" v-rule="'stencil_ticketEdit'" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" v-rule="'stencil_ticketDel'" @click="handleDelete(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <messageForm ref="messageFormRef" @update-list="getList" />
  </div>
  
</template>